<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="GmSlopeData1Form">
          <a-row>
						<a-col :span="24">
							<a-form-item label="地理位置" v-bind="validateInfos.position" id="GmSlopeData1Form-position" name="position">
								<a-input v-model:value="formData.position" placeholder="请输入地理位置"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="省" v-bind="validateInfos.province" id="GmSlopeData1Form-province" name="province">
								<a-input v-model:value="formData.province" placeholder="请输入省" disabled allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="市" v-bind="validateInfos.city" id="GmSlopeData1Form-city" name="city">
								<a-input v-model:value="formData.city" placeholder="请输入市" disabled allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="区县" v-bind="validateInfos.county" id="GmSlopeData1Form-county" name="county">
								<a-input v-model:value="formData.county" placeholder="请输入区县"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="乡镇" v-bind="validateInfos.town" id="GmSlopeData1Form-town" name="town">
								<a-input v-model:value="formData.town" placeholder="请输入乡镇"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="村" v-bind="validateInfos.village" id="GmSlopeData1Form-village" name="village">
								<a-input v-model:value="formData.village" placeholder="请输入村"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="组" v-bind="validateInfos.team" id="GmSlopeData1Form-team" name="team">
								<a-input v-model:value="formData.team" placeholder="请输入组"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="经度" v-bind="validateInfos.latitude" id="GmSlopeData1Form-latitude" name="latitude">
								<a-input-number v-model:value="formData.latitude" placeholder="请输入经度" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="纬度" v-bind="validateInfos.longitude" id="GmSlopeData1Form-longitude" name="longitude">
								<a-input-number v-model:value="formData.longitude" placeholder="请输入纬度" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="户主姓名" v-bind="validateInfos.householder" id="GmSlopeData1Form-householder" name="householder">
								<a-input v-model:value="formData.householder" placeholder="请输入户主姓名"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡类型" v-bind="validateInfos.slopeType" id="GmSlopeData1Form-slopeType" name="slopeType">
								<j-dict-select-tag v-model:value="formData.slopeType" dictCode="slope_type" placeholder="请选择切坡类型"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="土层松散程度" v-bind="validateInfos.soilLooseness" id="GmSlopeData1Form-soilLooseness" name="soilLooseness">
								<j-dict-select-tag v-model:value="formData.soilLooseness" dictCode="soil_looseness" placeholder="请选择土层松散程度"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="上覆土体类型" v-bind="validateInfos.overlyingSoilType" id="GmSlopeData1Form-overlyingSoilType" name="overlyingSoilType">
								<j-dict-select-tag v-model:value="formData.overlyingSoilType" dictCode="overlying_soil_type" placeholder="请选择上覆土体类型"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="岩体特征" v-bind="validateInfos.rockCharacteristics" id="GmSlopeData1Form-rockCharacteristics" name="rockCharacteristics">
								<j-dict-select-tag v-model:value="formData.rockCharacteristics" dictCode="rock_characteristics" placeholder="请选择岩体特征"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="裂隙发育程度" v-bind="validateInfos.fractureDevelopmentDegree" id="GmSlopeData1Form-fractureDevelopmentDegree" name="fractureDevelopmentDegree">
								<j-dict-select-tag v-model:value="formData.fractureDevelopmentDegree" dictCode="fracture_development_degree" placeholder="请选择裂隙发育程度"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡高度" v-bind="validateInfos.cutSlopeHeight" id="GmSlopeData1Form-cutSlopeHeight" name="cutSlopeHeight">
								<j-dict-select-tag v-model:value="formData.cutSlopeHeight" dictCode="cut_slope_height" placeholder="请选择切坡高度"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡坡度" v-bind="validateInfos.cutSlopeGradient" id="GmSlopeData1Form-cutSlopeGradient" name="cutSlopeGradient">
								<j-dict-select-tag v-model:value="formData.cutSlopeGradient" dictCode="cut_slope_gradient" placeholder="请选择切坡坡度"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡坡形" v-bind="validateInfos.cutSlopeShape" id="GmSlopeData1Form-cutSlopeShape" name="cutSlopeShape">
								<j-dict-select-tag v-model:value="formData.cutSlopeShape" dictCode="cut_slope_shape" placeholder="请选择切坡坡形"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="自然斜坡坡度" v-bind="validateInfos.naturalSlopeGradient" id="GmSlopeData1Form-naturalSlopeGradient" name="naturalSlopeGradient">
								<j-dict-select-tag v-model:value="formData.naturalSlopeGradient" dictCode="natural_slope_gradient" placeholder="请选择自然斜坡坡度" disabled allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="岩组类型" v-bind="validateInfos.rockType" id="GmSlopeData1Form-rockType" name="rockType">
								<j-dict-select-tag v-model:value="formData.rockType" dictCode="" placeholder="请选择岩组类型" disabled allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="植被覆盖率" v-bind="validateInfos.vegetationCoverage" id="GmSlopeData1Form-vegetationCoverage" name="vegetationCoverage">
								<j-dict-select-tag v-model:value="formData.vegetationCoverage" dictCode="vegetation_coverage" placeholder="请选择植被覆盖率"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="已有治理措施" v-bind="validateInfos.governanceMeasures" id="GmSlopeData1Form-governanceMeasures" name="governanceMeasures">
								<j-dict-select-tag v-model:value="formData.governanceMeasures" dictCode="governance_measures" placeholder="请选择已有治理措施"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡墙距" v-bind="validateInfos.slopeWallDistance" id="GmSlopeData1Form-slopeWallDistance" name="slopeWallDistance">
								<j-dict-select-tag v-model:value="formData.slopeWallDistance" dictCode="slope_wall_distance" placeholder="请选择切坡墙距"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度" v-bind="validateInfos.cutSlopeWidth" id="GmSlopeData1Form-cutSlopeWidth" name="cutSlopeWidth">
								<j-dict-select-tag v-model:value="formData.cutSlopeWidth" dictCode="cut_slope_width" placeholder="请选择切坡宽度"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡时间" v-bind="validateInfos.cutSlopeTime" id="GmSlopeData1Form-cutSlopeTime" name="cutSlopeTime">
								<j-dict-select-tag v-model:value="formData.cutSlopeTime" dictCode="cut_slope_time" placeholder="请选择切坡时间"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有人居" v-bind="validateInfos.resident" id="GmSlopeData1Form-resident" name="resident">
								<j-dict-select-tag v-model:value="formData.resident" dictCode="resident" placeholder="请选择是否有人居"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口" v-bind="validateInfos.threatPopulation" id="GmSlopeData1Form-threatPopulation" name="threatPopulation">
								<j-dict-select-tag v-model:value="formData.threatPopulation" dictCode="threat_population" placeholder="请选择威胁人口"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构" v-bind="validateInfos.threatHouse" id="GmSlopeData1Form-threatHouse" name="threatHouse">
								<j-dict-select-tag v-model:value="formData.threatHouse" dictCode="threat_house" placeholder="请选择威胁房屋结构"  allow-clear />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../GmSlopeData1.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    position: '',   
    province: '江西省',   
    city: '赣州市',   
    county: '',   
    town: '',   
    village: '',   
    team: '',   
    latitude: undefined,
    longitude: undefined,
    householder: '',   
    slopeType: '',   
    soilLooseness: '',   
    overlyingSoilType: '',   
    rockCharacteristics: '',   
    fractureDevelopmentDegree: '',   
    cutSlopeHeight: '',   
    cutSlopeGradient: '',   
    cutSlopeShape: '',   
    naturalSlopeGradient: '',   
    rockType: '',   
    vegetationCoverage: '',   
    governanceMeasures: '',   
    slopeWallDistance: '',   
    cutSlopeWidth: '',   
    cutSlopeTime: '',   
    resident: '',   
    threatPopulation: '',   
    threatHouse: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    province: [{ required: true, message: '请输入省!'},],
    city: [{ required: true, message: '请输入市!'},],
    county: [{ required: true, message: '请输入区县!'},],
    town: [{ required: true, message: '请输入乡镇!'},],
    village: [{ required: true, message: '请输入村!'},],
    team: [{ required: true, message: '请输入组!'},],
    latitude: [{ required: true, message: '请输入经度!'},],
    longitude: [{ required: true, message: '请输入纬度!'},],
    slopeType: [{ required: true, message: '请输入切坡类型!'},],
    cutSlopeHeight: [{ required: true, message: '请输入切坡高度!'},],
    cutSlopeGradient: [{ required: true, message: '请输入切坡坡度!'},],
    cutSlopeShape: [{ required: true, message: '请输入切坡坡形!'},],
    naturalSlopeGradient: [{ required: true, message: '请输入自然斜坡坡度!'},],
    rockType: [{ required: true, message: '请输入岩组类型!'},],
    vegetationCoverage: [{ required: true, message: '请输入植被覆盖率!'},],
    governanceMeasures: [{ required: true, message: '请输入已有治理措施!'},],
    slopeWallDistance: [{ required: true, message: '请输入切坡墙距!'},],
    cutSlopeWidth: [{ required: true, message: '请输入切坡宽度!'},],
    cutSlopeTime: [{ required: true, message: '请输入切坡时间!'},],
    resident: [{ required: true, message: '请输入是否有人居!'},],
    threatPopulation: [{ required: true, message: '请输入威胁人口!'},],
    threatHouse: [{ required: true, message: '请输入威胁房屋结构!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
